<?php

use app\models\Manager;

$currentManager = Manager::getCurrentManager();
$isSystem = false;
if ($currentManager['institution_code'] == '0') {
    $isSystem = true;
}
?>

<?php
if ($isSystem == true) {
    ?>
    <div class="panel panel-default">
        <div class="panel-body">
            <div class="form-inline">
                <div class="pull-right">
                    <div class="form-group">
                        <?= app\widgets\InstitutionHierarchyAutocompleteWidget::widget(['id' => 'searchInputInstitution', 'currentInstitutionCode' => $current_institution_code]) ?>
                    </div>
                    <div class="btn-group">
                        <button id="btnSearch" class="btn btn-primary"><i class="fa fa-search"></i> 搜索</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <?php
}
?>

<div class="panel panel-default">
    <div class="panel-heading">
        <div class="panel-title">
            应用接入安全信息
        </div>
    </div>
    <div class="panel-body">
        <div class="form-horizontal adminex-form">
            <div class="form-group">
                <label class="col-lg-2 control-label">Account:</label>
                <div class="col-lg-6">
                    <input type="text" class="form-control" id="inputAccount" disabled
                           value="<?= $item == null ? '' : $item['account'] ?>">
                </div>
                <button id="btnCreateAccountToken" class="btn btn-primary">生成</button>
            </div>
            <div class="form-group">
                <label class="col-lg-2 control-label">Token:</label>
                <div class="col-lg-6">
                    <input type="text" class="form-control" id="inputToken" disabled
                           value="<?= $item == null ? '' : $item['token'] ?>">
                </div>
            </div>
        </div>
    </div>
</div>

<div class="panel panel-default">
    <div class="panel-heading">
        <div class="panel-title">
            <div class="switch">
                <input type="checkbox" id="inputIsUserOpen"
                    <?php
                    if ($item != null && $item['is_user_open'] == 1) {
                        echo 'checked';
                    }
                    ?>/> 自有会员接入
            </div>
        </div>
    </div>
    <div class="panel-body">
        <div class="form-horizontal adminex-form">
            <div class="form-group">
                <label class="col-lg-2 control-label">登录页跳转地址:</label>
                <div class="col-lg-6">
                    <input class="form-control" type="text" id="inputUserLoginUrl"
                           value="<?= $item == null ? '' : $item['user_login_url'] ?>">
                </div>
            </div>
        </div>
    </div>
</div>

<div class="panel panel-default">
    <div class="panel-heading">
        <div class="panel-title">
            <div class="switch">
                <input type="checkbox" id="inputIsIntegralOpen"
                    <?php
                    if ($item != null && $item['is_integral_open'] == 1) {
                        echo 'checked';
                    }
                    ?>/> 自有积分接入
            </div>
        </div>
    </div>
    <div class="panel-body">
        <div class="form-horizontal adminex-form">
            <div class="form-group">
                <label class="col-lg-2 control-label">积分扣除接口地址:</label>
                <div class="col-lg-6">
                    <input class="form-control" type="text" id="inputSetIntegralUrl"
                           value="<?= $item == null ? '' : $item['set_integral_url'] ?>">
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-2 control-label">订单成功通知接口地址:</label>
                <div class="col-lg-6">
                    <input class="form-control" type="text" id="inputSetOrderUrl"
                           value="<?= $item == null ? '' : $item['set_order_url'] ?>">
                </div>
            </div>
        </div>
    </div>
</div>
<div class="panel panel-default">
    <div class="panel-heading">
        <div class="panel-title">
            <div class="switch">
                <input type="checkbox" id="inputIsAgent"
                    <?php
                    if ($item != null && $item['is_agent'] == 1) {
                        echo 'checked';
                    }
                    ?>/> 是否代理商
            </div>
        </div>
    </div>
</div>
<button class="btn btn-primary btn-lg col-lg-12" id="btnOk">确定</button>

<script type="application/javascript">
    $(function () {
        $('#btnSearch').click(function () {
            var searchInstitution = $('#searchInputInstitution').val();
            if (searchInstitution) {
                var url = window.location.href;
                url = setUrlParam(url, 'institution_code', searchInstitution);
                window.location.href = url;
            } else {
                alert('请选择机构!');
            }
        });

        $("#btnCreateAccountToken").click(function () {
            <?php
            if ($isSystem == true) {
            ?>
            if ($('#searchInputInstitution').val() == '0') {
                alert('请选择机构!');
                return;
            }
            var institutionCode = $('#searchInputInstitution').val();
            <?php
            } else {
            ?>
            var institutionCode = '<?= $currentManager['institution_code'] ?>';
            <?php
            }
            ?>

            loading.show();
            requestJson('create-account-token-handler', null, true, function (responseData) {
                loading.hide();
                if (responseData.status == 0) {
                    $("#inputAccount").val(responseData.data.account);
                    $("#inputToken").val(responseData.data.token);
                } else {
                    alert(responseData.message);
                }
            });
        });

        $('#btnOk').click(function () {
            <?php
            if ($isSystem == true) {
            ?>
            if ($('#searchInputInstitution').val() == '0') {
                alert('请选择机构!');
                return;
            }
            var institutionCode = $('#searchInputInstitution').val();
            <?php
            } else {
            ?>
            var institutionCode = '<?= $currentManager['institution_code'] ?>';
            <?php
            }
            ?>


            var isOk = confirm('确认保存?');
            if (isOk == true) {
                var data = {};
                data.institution_code = institutionCode;
                data.account = $('#inputAccount').val();
                data.token = $('#inputToken').val();
                data.is_user_open = $('#inputIsUserOpen').is(':checked') ? '1' : '0';
                data.user_login_url = $('#inputUserLoginUrl').val();
                data.is_integral_open = $('#inputIsIntegralOpen').is(':checked') ? '1' : '0';
                data.is_agent = $('#inputIsAgent').is(':checked') ? '1' : '0';
                data.set_integral_url = $('#inputSetIntegralUrl').val();
                data.set_order_url = $('#inputSetOrderUrl').val();
                loading.show();
                requestJson('set-open-config-handler', data, true, function (responseData) {
                    loading.hide();
                    if (responseData.status == 0) {
                        alert('保存成功!');
                    } else {
                        alert(responseData.message);
                    }
                });
            }
        });
    });
</script>

